package com.axx.android.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by sunjie on 2017/7/25.
 */

public class DateUtils {
    public static String formatTime(Long ms) {
        if (ms < 1000) {
            return "1秒";
        }
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }
        return sb.toString();
    }

    public static String getDiffTime(String endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date endTimeDate = simpleDateFormat.parse(endTime);
            long diff = endTimeDate.getTime() - System.currentTimeMillis();
            return getHourString(diff);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getHourString(long time) {
        if (time == 0){
            return "0.01h";
        }
        Integer hh = 1000 * 60 * 60;
        return String.format(Locale.US, "%.2fh", (double) time / hh);
    }
}
